<template>
  <div class="app-container customer-div">
    <div style="margin-top:40px">
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="年份：">
           <el-select v-model="listQuery.year">
              <el-option :value="item" :label="item" :key="item" v-for="item in yearList"></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="客户编号：">
           <el-input v-model="listQuery.customerNumber"></el-input>
<!--          <el-autocomplete-->
<!--            clearable-->
<!--            class="inline-input"-->
<!--            value-key="customerName"-->
<!--            :fetch-suggestions="querySearch"-->
<!--            v-model="listQuery.customerName"-->
<!--            placeholder="请输入内容"-->
<!--            @select="handleSelectCustomerName"-->
<!--            :trigger-on-focus="false"-->
<!--          ></el-autocomplete>-->
        </el-form-item>

        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>

        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-radio-group v-model="listQuery.goodsName" size="small" style="margin-left:20px" @change="handleSearchList">
        <el-radio-button label="">总量购买频率</el-radio-button>
        <el-radio-button label="PA6">PA6购买频率</el-radio-button>
        <el-radio-button label="PA66">PA66购买频率</el-radio-button>
        <el-radio-button label="PC">PC购买频率</el-radio-button>
        <el-radio-button label="ABS">ABS购买频率</el-radio-button>
      </el-radio-group>
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          label="客户编号"
          min-width="150"
          align="center">
          <template slot-scope="scope">
            <el-link @click="toDetail(scope.row)" type="primary" style="font-size:12px">
              {{scope.row.customerNumber}}
            </el-link>
            <!--是否有权限-->
            <div v-if="scope.row.isShowCustomerName">{{scope.row.customerName}}</div>
          </template>
        </el-table-column>
        <el-table-column  min-width="100"  align="center" v-for="item in 12" :key="item">
          <template slot="header" slot-scope="scope" >
            <span v-if="item < 10">{{listQuery.year + '-0' +item}}</span>
            <span v-else>{{listQuery.year + '-' +item}}</span>
          </template>
          <template slot-scope="scope">
            <span v-if="item < 10">{{scope.row[listQuery.year + '-0' + item]}}</span>
            <span v-else>{{scope.row[listQuery.year + '-' +item]}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="合计"
          min-width="100"
          align="center">
          <template slot-scope="scope">
              {{scope.row.allBuyNum}}
          </template>
        </el-table-column>


      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>




  </div>
</template>
<script>
import axios from 'axios'
import {getCustomerOption} from '@/api/partner'
import {formatDate} from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
import {
  buyRateStatistics
} from "../../api/partner";

const defaultListQuery={
  "year" : "",
  "customerName" : "",
  "goodsName" : "",
  "chargeId" : 0,  //业务员ID
  "pageNum"  : 1,
  "pageSize" : 10,
  customerNumber:''
};



export default {
  name: "index",
  data(){
    return {
      roles:store.getters.roles ? store.getters.roles : [],
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      yearList:[],
    }
  },
  computed: {

  },
  created() {
    let nowYaer = new Date().getFullYear();
    for(let i = nowYaer;i>=2018;i--){
      this.yearList.push(i)
    }
    this.listQuery.year = nowYaer;
    this.getList();
  },
  filters: {

  },
  methods:{
    //去详情页
    toDetail(item){
      // let routeData = this.$router.resolve({name:'customerDetail',query:{id:item.customerId}});
      // window.open(routeData.href);
      this.$router.push({name:'customerDetail',query:{id:item.customerId}})
    },
    //客户名称输入匹配
    querySearch(queryString, cb){
      let data = {};
      data.keywords = queryString;
      getCustomerOption(data).then((res)=>{
        cb(res);
      })
    },

    //选择客户名称
    handleSelectCustomerName(item){
      this.listQuery.customerName = item.customerName
    },

    //获取列表1
    getList(){
      this.listLoading = true;
      let listQuery = JSON.parse(JSON.stringify(this.listQuery));
      buyRateStatistics(listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },


  },
}
</script>

<style scoped lang="scss">
.upload-div{
  text-align: center;
  position:relative;
  #upload{

    opacity: 0;
    position: absolute;
    z-index: 1;
    width:154px;
    height:40px
  }
}
.title-tab{
  border-radius: 8px;
  background: #eee;
  margin-left: 10px;
  display: inline-block;
  vertical-align: middle;
  line-height: 20px;
  padding: 0 5px;
  color: #666;
}
</style>
